package com.syqc.videos

import android.view.View
import androidx.core.view.isVisible
import com.gyf.immersionbar.ktx.immersionBar
import com.syqc.comlib.base.BaseActivity
import com.syqc.comlib.utils.MLog
import com.syqc.utils.Keys
import com.shuyu.gsyvideoplayer.GSYVideoManager
import com.shuyu.gsyvideoplayer.builder.GSYVideoOptionBuilder
import com.shuyu.gsyvideoplayer.listener.GSYSampleCallBack
import com.shuyu.gsyvideoplayer.utils.GSYVideoType
import com.syqc.monitor.databinding.ActivityPlayDownBinding

class PlayDownActivity : BaseActivity<ActivityPlayDownBinding>(ActivityPlayDownBinding::inflate) {

    private var isPlay = false
    private var isPause = false

    override fun initView() {
        val url = intent.getStringExtra(Keys.INTENT_DATA)
        val title = intent.getStringExtra(Keys.INTENT_TITLE) ?: ""
        //增加title
        binding.detailPlayer.titleTextView.isVisible = true
        binding.detailPlayer.backButton.isVisible = true
        GSYVideoType.setShowType(GSYVideoType.SCREEN_TYPE_16_9)
        MLog.e("url====$url")
        GSYVideoOptionBuilder()
            .setIsTouchWiget(true)
            .setRotateViewAuto(false)
            .setLockLand(false)
            .setAutoFullWithSize(false)
            .setShowFullAnimation(false)
            .setNeedLockFull(true)
            .setUrl(url)
            .setCacheWithPlay(true)
            .setVideoTitle(title) ///不需要旋转
            .setNeedOrientationUtils(true)
            .setVideoAllCallBack(
                object : GSYSampleCallBack() {
                    override fun onPrepared(url: String, vararg objects: Any) {
                        super.onPrepared(url, *objects)
                        isPlay = true
                    }

                    override fun onQuitFullscreen(url: String, vararg objects: Any) {
                        super.onQuitFullscreen(url, *objects)
                    }
                }).setLockClickListener { view, lock -> }.build(binding.detailPlayer)
        binding.detailPlayer.backButton.setOnClickListener {
            finish()
        }
        binding.detailPlayer.fullscreenButton.setOnClickListener { //直接横屏
            binding.detailPlayer.startWindowFullscreen(this@PlayDownActivity, true, true)
        }
    }

    override fun setListener() {

    }

    override fun onBackPressed() {
        if (GSYVideoManager.backFromWindowFull(this)) {
            return
        }
        super.onBackPressed()
    }


    override fun onPause() {
        binding.detailPlayer.currentPlayer.onVideoPause()
        super.onPause()
        isPause = true
    }

    override fun onResume() {
        binding.detailPlayer.currentPlayer.onVideoResume(false)
        super.onResume()
        isPause = false
    }

    override fun onDestroy() {
        super.onDestroy()
        if (isPlay) {
            binding.detailPlayer.currentPlayer.release()
        }
    }
    override fun onWindowFocusChanged(hasFocus: Boolean) {
        super.onWindowFocusChanged(hasFocus)
        window.decorView.systemUiVisibility =
            View.SYSTEM_UI_FLAG_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or
                    View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
    }
}